#!/usr/bin/env python3
"""
LightPort 使用示例 - 基础扫描
"""

import asyncio
import sys
from pathlib import Path

# 添加项目根目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent))

from core.scanner import LightScanner

async def basic_scan_example():
    """基础扫描示例"""
    
    # 创建扫描器实例
    scanner = LightScanner(
        stealth_mode=False,
        bypass_enabled=False,
        timeout=3,
        max_threads=50,
        verbose=True
    )
    
    # 定义目标
    targets = ["127.0.0.1"]  # 本地测试
    ports = [22, 80, 443, 3306, 5432]
    
    print("开始基础扫描...")
    print(f"目标: {targets}")
    print(f"端口: {ports}")
    
    # 执行扫描
    results = await scanner.scan_targets(targets, ports)
    
    # 显示结果
    print("\n扫描结果:")
    for target, ports_info in results['results'].items():
        print(f"\n目标: {target}")
        for port_info in ports_info:
            print(f"  端口 {port_info['port']}: {port_info['status']} "
                  f"({port_info.get('service', 'unknown')})")

async def stealth_scan_example():
    """隐蔽扫描示例"""
    
    scanner = LightScanner(
        stealth_mode=True,
        bypass_enabled=True,
        timeout=5,
        max_threads=20,
        verbose=True
    )
    
    targets = ["192.168.1.1"]  # 路由器测试
    ports = list(range(1, 100))
    
    print("\n开始隐蔽扫描...")
    results = await scanner.scan_targets(targets, ports)
    
    # 统计开放端口
    open_ports = []
    for target_ports in results['results'].values():
        open_ports.extend([p['port'] for p in target_ports 
                          if p['status'] == 'open'])
    
    print(f"\n发现开放端口: {sorted(open_ports)}")

if __name__ == "__main__":
    print("LightPort 使用示例")
    print("=" * 50)
    
    try:
        # 运行基础扫描示例
        asyncio.run(basic_scan_example())
        
        # 运行隐蔽扫描示例
        asyncio.run(stealth_scan_example())
        
    except KeyboardInterrupt:
        print("\n扫描被中断")
    except Exception as e:
        print(f"错误: {e}")
